package com.example.capsule_netty.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.capsule_netty.bean.*;
import com.example.capsule_netty.configuration.Constant;
import com.example.capsule_netty.global.UserAndChannelRelevance;
import com.example.capsule_netty.mapper.RoomMapper;
import com.example.capsule_netty.mapper.UserInfoMapper;
import com.google.gson.Gson;
import io.netty.channel.Channel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * @Author: zhuzhiming
 * @Date: 2022/5/2 0:19
 */
@Service
public class RoomService {

    @Resource
    RoomMapper roomMapper;

    @Autowired
    UserAndChannelRelevance userAndChannelRelevance;

    @Resource
    UserInfoMapper userInfoMapper;

    @Autowired
    RedisTemplate redisTemplate;

    public List<RoomBean> getRoomListByField(String field) {
        return roomMapper.getRoomListByField(field);
    }

    public String getChatRoomFriend(String roomName) {

        List<String> userIdList = userAndChannelRelevance.doGetChatRoomFriend(roomName);

        List<ChatGroupFriendBean> chatGroupFriendBeanList = new ArrayList<>();
        userIdList.forEach(userId -> {
            UserInfoBean userInfoBean = userInfoMapper.selectById(userId);
            ChatGroupFriendBean chatGroupFriendBean = new ChatGroupFriendBean();
            BeanUtils.copyProperties(userInfoBean, chatGroupFriendBean);
            chatGroupFriendBean.setSenderId(userId);
            chatGroupFriendBeanList.add(chatGroupFriendBean);
        });
        return new Gson().toJson(chatGroupFriendBeanList);
    }

    public String doSearchGroupFriend(String roomName, String content) {

        List<String> userIdList = userAndChannelRelevance.doGetChatRoomFriend(roomName);
        Set<ChatGroupFriendBean> set = new HashSet<>();
        userIdList.forEach(userId -> {
            UserInfoBean userInfoBean = userInfoMapper.selectById(userId);
            if (content.contains(userId) || content.contains(userInfoBean.getNickname())) {
                ChatGroupFriendBean chatGroupFriendBean = new ChatGroupFriendBean();
                BeanUtils.copyProperties(userInfoBean, chatGroupFriendBean);
                chatGroupFriendBean.setSenderId(userId);
                set.add(chatGroupFriendBean);
            }
        });

        ArrayList<ChatGroupFriendBean> chatGroupFriendBeanArrayList = new ArrayList<>(set);
        return new Gson().toJson(chatGroupFriendBeanArrayList);
    }

    public String doStatisticPeople(String roomName, String imei) {

        CompletableFuture.runAsync(() -> {
            userAndChannelRelevance.doAddRoomPeopleCount(roomName, imei);
        });

        HashSet<Channel> channels = userAndChannelRelevance.getChannels(roomName);
        return new Gson().toJson(channels.size());

    }

    public String doAddSecretRoom(MessageBean messageBean) {

        String senderId = messageBean.getSenderId();
        String roomName = messageBean.getRoomName();
        String password = messageBean.getContent();
        String field = messageBean.getField();
        String key = field + "-" + roomName + "-" + "secretMember";
        Boolean member = redisTemplate.opsForSet().isMember(key, senderId);
        if (Boolean.TRUE.equals(member)) {
            messageBean.setContent("不可重复加入");
            return new Gson().toJson(messageBean);
        }
        QueryWrapper<RoomBean> wrapper = new QueryWrapper<RoomBean>().eq("field", field).eq("room_name", roomName).eq("password", password);
        List<RoomBean> roomBeanList = roomMapper.selectList(wrapper);
        if (roomBeanList == null || roomBeanList.size() == 0) {
            messageBean.setContent("聊天室名字或者密码错误");
            return new Gson().toJson(messageBean);
        }
        if (roomBeanList.get(0).getSecretLevel() == 1) {
            redisTemplate.opsForSet().add(key, senderId);
        }
        messageBean.setContent("加入成功");
        return new Gson().toJson(messageBean);
    }
}
